package com.breaktian.assemble.customviews.photo.photoscan;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.breaktian.assemble.utils.LogUtil;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by tiankeke on 2017/6/15.
 * 把page的count设置Ingeter最大值
 * 用position对数据源size取摸
 */

public abstract class PhotoScanPagerAdapter<T> extends PagerAdapter {

    private final Context mContext;

    private List<T> mDatas = new ArrayList<>();

    public PhotoScanPagerAdapter(Context context, List<T> data){
        this.mContext = context;
        this.mDatas.clear();
        this.mDatas.addAll(data);
    }

    public void setData(List<T> data){
        this.mDatas.clear();
        this.mDatas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

//        final ImageView view = (ImageView) mViews.get(viewPos);
        final ImageView view = new PhotoView(mContext);

        String url = getImageUrl(mDatas.get(position));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoScanPagerAdapter.this.onClick(mDatas.get(position),view,position);
            }
        });
        LogUtil.d("正在生成第几页："+position+",正在调用mData的位置："+position);
        loadImage(view,url);
        ((ViewPager)container).addView(view);
        return view;
    }

    /**
     * 此方法抽象出来
     * */
    public abstract void loadImage(ImageView imageView, String url);
    public abstract String getImageUrl(T t);
    public abstract void onClick(T t,View view,int postion);







    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        int viewPos = Math.abs(position % VIEW_SIZE);
//        View view = mViews.get(viewPos);
//        LogUtil.d("正在销毁第几页："+position+",正在销毁对应mViews的位置："+viewPos);
        LogUtil.d("正在销毁第几页："+position);
        container.removeView((View)object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
